package com.itheima.web.servlet;

import com.alibaba.fastjson.JSON;
import com.itheima.pojo.Brand;
import com.itheima.pojo.PageBean;
import com.itheima.service.BrandService;
import com.itheima.service.impl.BrandServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;

/**
 * BrandServlet里面就定义关于Brand的方法
 */

/**
 * 通过/brand/*来访问brandServlet里面的方法，在base里面我们去拿到方法名，通过反射去执行方法
 */
@WebServlet("/brand/*")
public class BrandServlet extends BaseServlet {

    /**
     * 查询所有
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void selectAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //调用业务层方法
        //使用多态方式创建业务对象，解除servlet和service的耦合
        BrandService service=new BrandServiceImpl();

        List<Brand> brands = service.selectAll();

        //将java对象数组转为json数组
        String s = JSON.toJSONString(brands);

        //数据中含有中文，要设置输出编码,输出数据类型为text/json
        response.setContentType("text/json;charset=utf-8");
        //返回json数据
        response.getWriter().write(s);
    }

    /**
     * 添加品牌
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        //从前端获取传来的json数据
        //使用request输入流
        BufferedReader reader = request.getReader();
        String s = reader.readLine();
        System.out.println(s);

        //将json数据转为brand对象
        Brand brand = JSON.parseObject(s, Brand.class);

        //调用service层方法
        BrandService service=new BrandServiceImpl();
        service.insert(brand);

        //通过response将插入是否成功的布尔值返回
        response.getWriter().write("success");
    }

    public void deleteByIds(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        //获取前端传来的json数组【1,2,3】
        BufferedReader reader = request.getReader();
        String s = reader.readLine();
        System.out.println(s);
        //将json数组转为int数组
        int[] ids = JSON.parseObject(s, int[].class);
        //调用业务层的方法
        BrandService service=new BrandServiceImpl();
        service.deleteByIds(ids);

        //给前端响应数据
        response.getWriter().write("success");

    }



    public void selectByPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //获取前端传入的当前页数和每页显示数量
        String _currentPage = request.getParameter("currentPage");
        String _pageSize = request.getParameter("pageSize");
        //将String转为int
        int currentPage = Integer.parseInt(_currentPage);
        int pageSize = Integer.parseInt(_pageSize);


        //调用业务层方法
        BrandService service=new BrandServiceImpl();
        PageBean<Brand> pageBean = service.selectByPages(currentPage, pageSize);

        //将java对象转为json数据
        String s = JSON.toJSONString(pageBean);

        //数据中含有中文，要设置输出编码,输出数据类型为text/json
        response.setContentType("text/json;charset=utf-8");
        //返回json数据
        response.getWriter().write(s);
    }
}
